package com.woniu.project.web.config;


import com.woniu.project.web.dto.DocDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * 配置Swagger2 生成接口文档
 * @Author: Klaus
 * @Date: 2020/9/28 19:53
 */
@Configuration
@EnableSwagger2
@EnableConfigurationProperties(value = DocDto.class)
public class SwaggerConfig {

    @Autowired
    private DocDto docDto;

    @Bean
    public Docket creatDocket(){
        Docket docket = new Docket(DocumentationType.SWAGGER_2);

        ParameterBuilder tokenPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        tokenPar.name("X-Token").description("管理员token").modelRef(new ModelRef("string")).parameterType("header").required(false).build();
        pars.add(tokenPar.build());
        
        docket.apiInfo(new ApiInfoBuilder()
                .title(docDto.getTitle())
                .description(docDto.getDesc())
                .contact(new Contact(docDto.getAuthor().getName()
                        ,docDto.getAuthor().getUrl()
                        ,docDto.getAuthor().getEmal()))
                .version(docDto.getVersion())
                .build())
                .select()
                .apis(RequestHandlerSelectors.basePackage(docDto.getBasePath()))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
        return docket;
    }

}
